package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.largeScreen.vo.IntensityMapVo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.WaterSysEquipment;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.WaterSysLossRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/15
 * @Description: 水利系统设施、设备地震损失报表
 */
@Repository
public class WaterSysLossRepositoryImpl implements WaterSysLossRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_loss_water_sys_equipment"; // 表名

    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  "+TABLE_NAME+"  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
    }

    @Override
    public void batchSave(List<WaterSysEquipment> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+TABLE_NAME +
                " ( id,xlrm_id,fill_form_user,tel,fill_form_time,province,city,county,township,facility," +
                "scale,dam_type,original_value,place,damage_condition_desc,damage,destroy,ruined," +
                "urgent_repair,estimation_relied_on,estimation_loss,total_loss,subitem,sequence,comment,enterprise_name)" +
                "values (:id,:xlrmId,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:township,:facility," +
                ":scale,:damType,:originalValue,:place,:damageConditionDesc,:damage,:destroy,:ruined," +
                ":urgentRepair,:estimationReliedOn,:estimationLoss,:totalLoss,:subitem,:sequence,:comment,:enterpriseName)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<WaterSysEquipment> getWaterSysLossData(String xsemId, String waterSysLossEquipment) {
        String sql = "select t1.*  from xczh_loss_water_sys_equipment " +
                "t1 left join xczh_loss_report_manage t2 on t1.xlrm_id = t2.id  " +
                "where t2.report_type= ? " +
                "and xsem_id= ?";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WaterSysEquipment.class),waterSysLossEquipment, xsemId);
    }
}
